﻿using RabbitMQ.Client;
using System;
using System.Text;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Start");
            //1.实例化连接工厂
            IConnectionFactory conFactory = new ConnectionFactory//创建连接工厂对象
            {
                HostName = "localost",//IP地址
                Port = 5672,//端口号
                UserName = "admin",//用户账号
                Password = "admin"//用户密码
            };
            //2. 建立连接
            using (IConnection con = conFactory.CreateConnection())//创建连接对象
            {
                //3. 创建信道
                using (IModel channel = con.CreateModel())//创建连接会话对象
                {
                   

                    String queueName = String.Empty;
                    if (args.Length > 0)
                        queueName = args[0];
                    else
                        queueName = "queue1";
                    //4. 申明队列  我已经通过ui 手动创建了队列
                    //channel.QueueDeclare(
                    //queue: queueName,//消息队列名称
                    //durable: false,//是否缓存
                    //exclusive: false,
                    //autoDelete: false,
                    //arguments: null
                    //);
                    while (true)
                    {
                        Console.WriteLine("消息内容:");
                        String message = Console.ReadLine();
                        //5. 构建byte消息数据包
                        byte[] body = Encoding.UTF8.GetBytes(message);
                        //6. 发送数据包
                        channel.BasicPublish(exchange: "", routingKey: queueName, basicProperties: null, body: body);
                        Console.WriteLine("成功发送消息:" + message);
                    }
                }
            }
         
        }
    }
}
